#!/usr/bin/env python
# encoding: utf-8
'''
@author: liuchong
@contact: lucas@007vin.com
@software: garner
@file: banzhou.py
@time: 2020/8/24 2:44 下午
@desc:
'''

import sys
sys.path.append('../../')
import pandas as pd
import ujson
import re

from pymongo import MongoClient

from models.seat.seat import CarsPartNumNew, CarsSaDesc
from models.seat.seat_vb import Vb
from scripts.fenci import remove_tt
vwag_parts = CarsPartNumNew.select(CarsPartNumNew.pid).where(CarsPartNumNew.pid != '').distinct().order_by(CarsPartNumNew.pid).limit(70000).offset(70000*4)

s = ['2缸', '二缸', '3缸', '三缸', '4缸', '四缸', '5缸', '五缸', '6缸', '六缸', '8缸', '八缸', '10缸', '十缸', '12缸', '十二缸']
qd = ['全轮驱动', '前驱动', '前部驱动', '四轮驱动']
bsx = ['7档', '7档自动', '7档手动', '7档双离合', '七档', '七档自动', '七档手动', '七档双离合', '6档', '6档自动', '6档手动', '6挡手自一体',
       '六档', '六档自动', '六档手动', '5档', '5档自动', '5档手动', '五档', '五档自动', '五档手动', '5挡手自一体',
       '4档', '4档自动', '4档手动', '四档', '四档自动', '四档手动', '8档', '8档自动', '8档手动', '8挡手自一体',
       '八档', '八档自动', '八档手动', '3档', '3档自动', '3档手动', '三档', '三档自动', '三档手动', '无级自动']
pl = ['1.6', '1.9', '2.0', '1.8', '1.3', '1.05', '1.4', '1.7', '2.3', '2.8', '1.0', '1.2', '1.5']
fdjdm = ['AAZ', 'ABF', 'ABN', 'ABS', 'ACC', 'ADZ', 'AGG', 'AHU', 'AFN', 'AFT', 'AAU', 'AAV', 'ABD', 'ABU', 'ADL', 'AEX', 'AEY', 'AEE', 'AHB', 'AKV', 'APQ', 'AUA', 'AUD', 'AYQ', 'ADY', 'AJH', 'AVG', 'AEH', 'AGN', 'AGR', 'AGZ', 'AHF', 'AHW', 'AJQ', 'AKL', 'ALH', 'APE', 'APG', 'APP', 'AQM', 'AQN', 'ARL', 'ARY', 'ASV', 'AUE', 'AUQ', 'AUS', 'AXP', 'AZD', 'AER', 'AFK', 'AHT', 'AKK', 'AKU', 'ALD', 'ALL', 'AMF', 'ANV', 'ANW', 'AQQ', 'AUB', 'AUC', 'AGP', 'ALM', 'APF', 'AQX', 'AST', 'AUR', 'AVZ', 'AYP', 'AFH', 'ANX', 'ANU', 'ATM', 'AUY', 'AWC', 'AYL', 'ASY', 'ASZ', 'ATD', 'AZL', 'AZQ', 'BAH', 'BBU', 'BBX', 'BBY', 'BBZ', 'BJX', 'BKV', 'BKY', 'BLZ', 'BME', 'BPX', 'AMK', 'AXR', 'BAM', 'BCA', 'BCB', 'BDE', 'BFQ', 'AZV', 'BGU', 'BJB', 'BKC', 'BKD', 'BLR', 'BLS', 'BLY', 'BMM', 'BMN', 'BSE', 'BSF', 'BVY', 'BVZ', 'BWA', 'BXE', 'BXW', 'BYT', 'BZB', 'CAXC', 'CCSA', 'CDAA', 'CEGA', 'BWJ', 'BXF', 'CAYC', 'CBZB', 'CCZB', 'CDLA', 'CDLD', 'CGGB', 'CHGA', 'BBM', 'BLT', 'BMS', 'BMT', 'BNM', 'BNV', 'BTS', 'BUK', 'BXV', 'CHZJ', 'CHZL', 'CWVA', 'DADA', 'DBYA', 'DGTA', 'DGTD', 'DKJA', 'DKLA', 'DKRF', 'DLAA', 'DLAC', 'DPCA', 'BRT', 'BTB', 'BVK', 'CZDA', 'DFGA', 'DFHA', 'DKZA', 'DTSA', 'DTSB', 'DTUA', 'BXJ', 'BZG', 'CAVE', 'CAVF', 'CAYA', 'CAYB', 'CEKA', 'CFHD', 'CFWA', 'CGPA', 'CGPB', 'CLNA', 'CNKA', 'ALZ', 'BWE', 'CAGA', 'CAGC', 'CAHA', 'CDHA', 'CDHB', 'CDND', 'CFMA', 'CGLB', 'CJCA', 'CJCC', 'CFHC', 'CFJA', 'CLCB', 'CMXA', 'CAVA', 'CCZA', 'CFFA', 'CFFB', 'CFFE', 'CFGB', 'CFGC', 'CNWB', 'CTHA', 'CNUB', 'CTHF', 'CTJB', 'CHYA', 'CHYB', 'CPGA', 'CAXA', 'CBZA', 'CFNA', 'CGPC', 'CJZC', 'CJZD', 'CUSB', 'CXMA', 'CZCA', 'CJLB', 'CPTA', 'CTHE', 'CTJC', 'CHPA', 'CHZD', 'CJSA', 'CJSB', 'CJXA', 'CJXE', 'CJXH', 'CJZA', 'CJZB', 'CKFC', 'CLHA', 'CLHB', 'CMBA', 'CPWA', 'CRBC', 'CRGA', 'CRKB', 'CRLB', 'CRLD', 'CRMB', 'CRVA', 'CRVC', 'CUNA', 'CUPA', 'CXSA', 'CXXA', 'CXXB', 'CYVA', 'CYVB', 'CZEA', 'DBKA', 'CJXC', 'CJXG', 'DACA', 'DCYA', 'DDYA', 'DDYB', 'DFFA', 'DGTE', 'DHFA', 'DJGA', 'DKLB', 'DNUC', 'DNUE', 'DPBA', 'CHYC', 'CWVB', 'DFNA', 'DFNB', 'DGTC', 'DSGB', 'DSGD', 'CUVA', 'CUVC', 'CUWA', 'DEDA', 'DFLA', 'DFLD', 'DFMA', 'DJKA', 'DLTA', 'DLTC', 'DLUB', 'CHZB', 'CHZC', 'CUSA', 'CUTA', 'CYZA', 'DAJA', 'DKLD', 'DKRC', 'CZPB', 'DTTC', 'EBMA', 'DFYA', 'DGEA', 'DLAB', 'DNFB', 'DSTB', 'DSUD']
client = MongoClient('192.168.10.170', 27017)
db = client.standard_label
collection = db.standard_label_unique
item = []
flag = 0
for a in vwag_parts:
    # 配置代码
    sa_codes = []
    # 发动机排量
    cylinder = []
    # 发动机代码
    vb = []
    # 发动机排量信息
    vb_desc = []

    # model_desc = []
    transmission = []
    drive = []
    diff_mech = []
    # 匹配信息
    label_info = CarsPartNumNew.get_or_none(CarsPartNumNew.pid == a.pid)
    label = label_info.label
    if label:
        label = re.findall(r'[\u4e00-\u9fa5]', label)
        label = ''.join(label)
    stand_info = collection.find_one({'pid': a.pid, 'brandCode': 'seat'})
    if stand_info:
        stand_label = stand_info['standard_label']
    else:
        stand_label = ''
    # 处理audi_copy表中的label字段
    parts = CarsPartNumNew.select().where(CarsPartNumNew.pid == a.pid)
    for part in parts:
        cid = part.cid
        part_sa_codes = part.sa_code or '{}'
        part_sa_codes = ujson.loads(part_sa_codes)

        part_remark = part.remark or ''
        # 处理audi part表和structure表的sa_code

        for key, value in part_sa_codes.items():
            if value:
                sa_codes = sa_codes+value
        # print(sa_codes)

        #处理structure表和part表的model字段

        part_model = part.model
        part_model = part_model.replace('<br/>', '')

        # 筛选出structure表model字段中的缸数和排量信息
        part_out = remove_tt(part_model)
        part_remark_out = remove_tt(part_remark)

        if part_remark:
            for key in s:
                if key in part_remark_out:
                    key = key.replace('二缸', '2')
                    key = key.replace('2缸', '2')
                    key = key.replace('三缸', '3')
                    key = key.replace('3缸', '3')
                    key = key.replace('四缸', '4')
                    key = key.replace('4缸', '4')
                    key = key.replace('五缸', '5')
                    key = key.replace('5缸', '5')
                    key = key.replace('六缸', '6')
                    key = key.replace('6缸', '6')
                    key = key.replace('八缸', '8')
                    key = key.replace('8缸', '8')
                    key = key.replace('十缸', '10')
                    key = key.replace('10缸', '10')
                    key = key.replace('十二缸', '12')
                    key = key.replace('12缸', '12')
                    cylinder.append(key)

            for key in pl:
                if key in part_remark:
                    vb_desc.append(key)
        if part_model:
            for key in bsx:
                if key in part_out:
                    key = key.replace('三', '3')
                    key = key.replace('四', '4')
                    key = key.replace('五', '5')
                    key = key.replace('六', '6')
                    key = key.replace('七', '7')
                    key = key.replace('八', '8')
                    key = key.replace('档', '挡')
                    if key == '5挡':
                        key = '5挡手动'
                    if key == '6挡':
                        key = '6挡手动'

                    if key == '6挡自动':
                        key = '6挡AMT'
                    if key == '7挡自动':
                        key = '7挡双离合'
                    if key == '无级自动':
                        key = 'CVT无级变速'
                    transmission.append(key)
            for key in qd:
                if key in part_out:
                    key = key.replace('前驱动', '前置前驱')
                    key = key.replace('前部驱动', '前置前驱')
                    key = key.replace('四轮驱动', '全轮驱动')
                    drive.append(key)

            for key in s:
                if key in part_out:
                    key = key.replace('二缸', '2')
                    key = key.replace('2缸', '2')
                    key = key.replace('三缸', '3')
                    key = key.replace('3缸', '3')
                    key = key.replace('四缸', '4')
                    key = key.replace('4缸', '4')
                    key = key.replace('五缸', '5')
                    key = key.replace('5缸', '5')
                    key = key.replace('六缸', '6')
                    key = key.replace('6缸', '6')
                    key = key.replace('八缸', '8')
                    key = key.replace('8缸', '8')
                    key = key.replace('十缸', '10')
                    key = key.replace('10缸', '10')
                    key = key.replace('十二缸', '12')
                    key = key.replace('12缸', '12')
                    cylinder.append(key)
            for key in pl:
                if key in part_model:
                    vb_desc.append(key)
            for key in fdjdm:
                if key in part_model:
                    vb.append(key)
            if 'PR:' in part_model or 'PR-' in part_model or 'pr:' in part_model or 'pr-' in part_model:
                # 处理part_model中的PR
                part_pr = re.sub('[\u4e00-\u9fa5]', '', part_model)
                part_pr = part_pr.replace('PR:', '')
                part_pr = part_pr.replace('PR-', '')
                part_pr = part_pr.replace('+', ',')
                part_pr = part_pr.replace('/', ',')
                part_pr = part_pr.replace(':', ',')
                part_pr = part_pr.replace('_', ',')
                part_pr = part_pr.split(',')
                for j in part_pr:
                    sa_codes.append(j)

    sa_codes = set(sa_codes)
    sa_codes = list(sa_codes)

    vb = set(vb)
    vb = list(vb)
    # print(vb)

    # 筛选sa_code对应的描述信息
    for i in sa_codes:
        contrast = CarsSaDesc.get_or_none(CarsSaDesc.sa == i, CarsSaDesc.lang == 'zh', CarsSaDesc.brand_code == 'seat')
        if contrast and contrast.desc:
            # model_desc.append(contrast.desc)
            if '发动机' in contrast.desc:
                # 筛选出sa表desc中的排量信息
                for key in pl:
                    if key in contrast.desc:
                        vb_desc.append(key)
                for key in s:
                    if key in contrast.desc:
                        key = key.replace('二缸', '2')
                        key = key.replace('2缸', '2')
                        key = key.replace('三缸', '3')
                        key = key.replace('3缸', '3')
                        key = key.replace('四缸', '4')
                        key = key.replace('4缸', '4')
                        key = key.replace('五缸', '5')
                        key = key.replace('5缸', '5')
                        key = key.replace('六缸', '6')
                        key = key.replace('6缸', '6')
                        key = key.replace('八缸', '8')
                        key = key.replace('8缸', '8')
                        key = key.replace('十缸', '10')
                        key = key.replace('10缸', '10')
                        key = key.replace('十二缸', '12')
                        key = key.replace('12缸', '12')
                        if key not in cylinder:
                            cylinder.append(key)

            # 处理变速器配置
            if ('变速箱' in contrast.desc or '变速器' in contrast.desc) and ('档' in contrast.desc or '无级' in contrast.desc):
                for key in bsx:
                    if key in contrast.desc:
                        key = key.replace('三', '3')
                        key = key.replace('四', '4')
                        key = key.replace('五', '5')
                        key = key.replace('六', '6')
                        key = key.replace('七', '7')
                        key = key.replace('八', '8')
                        key = key.replace('档', '挡')
                        if key == '5挡':
                            key = '5挡手动'

                        if key == '6挡':
                            key = '6挡手动'
                        if key == '6挡自动':
                            key = '6挡AMT'
                        if key == '7挡自动':
                            key = '7挡双离合'
                        if key == '无级自动':
                            key = 'CVT无级变速'
                        transmission.append(key)

            # 处理驱动配置
            for key in qd:
                if key in contrast.desc:
                    key = key.replace('前驱动', '前置前驱')
                    key = key.replace('前部驱动', '前置前驱')
                    key = key.replace('四轮驱动', '全轮驱动')
                    drive.append(key)

            # 处理差速器配置
            if '差速' in contrast.desc or '减速' in contrast.desc:
                diff_mech.append(i)

    # 筛选model中发动机代码对应的排量信息
    for i in vb:
        # print(Vb.aa.zfill(3))
        # break
        vb_AF = Vb.get_or_none(Vb.ab.contains(i), Vb.brand == 'seat')
        if vb_AF and vb_AF.af:
            vb_desc.append(str(int(vb_AF.af)/100))

    # print(vb)
    vb_desc = set(vb_desc)
    vb_desc = list(vb_desc)
    vb_desc = ','.join(vb_desc)

    cylinder = set(cylinder)
    cylinder = list(cylinder)
    cylinder = ','.join(cylinder)

    transmission = set(transmission)
    transmission = list(transmission)
    transmission = ','.join(transmission)

    drive_info = []
    drive = set(drive)
    drive = list(drive)
    for i in drive:
        if i == '前置前驱':
            drive_info.append('两驱')
        if i == '全轮驱动':
            drive_info.append('四驱')
    drive_new = ','.join(drive_info)
    drive = ','.join(drive)

    diff_mech = set(diff_mech)
    diff_mech = list(diff_mech)
    diff_mech = ','.join(diff_mech)

    vb = ','.join(vb)
    unit = {'pid': a.pid, 'standard_label': stand_label, 'label': label, 'desc': '', 'engine_code': vb,
            'cylinder': cylinder, 'transmission': transmission, 'trans_desc': transmission, 'replace': '',
            'drive': drive, '差速器': diff_mech, 'displacement': vb_desc, 'brandCode': 'seat', 'drive_new': drive_new}
    print(unit)
    item.append(unit)

df = pd.DataFrame(item)
df['pid'] = df['pid'].astype('str')
df.to_csv('./export/seat_all05.csv', mode='a', index=False)


